﻿using System;
using System.IO;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TsamIRC
{
    class Log
    {
        // Member variables
        private String _buffer = null;
        private FileStream _fs = null;
        private StreamWriter _streamWriter = null;
        private String _sFileName = "irc.log";
        private int outCol, outRow, outHeight = 300;

        // Opens or creates a log file. If the file exist
        // then the content is cleared, in other words, the
        // log file only logs of the latest session.
        public Log()
        {
            _fs = File.Open(_sFileName, FileMode.OpenOrCreate, FileAccess.Write);
            _streamWriter = new StreamWriter(_fs, System.Text.Encoding.UTF8);
            
        }

        // We need to set the string together with the dates
        // then it logs the string into both the irc.log that
        // is or will be located in the default directory
        // of the IRCCLIENT.exe.
        public void logLine(String message, bool client, bool write)
        {
            DateTime date =  DateTime.Now;
            _buffer = date.ToString("ddd d MMM H:mm:ss");
            if (client)
            {
                _buffer += " GMT : client : ";
            }
            else
            {
                _buffer += " GMT : server : ";
            }
            _buffer += message;
            _streamWriter.WriteLine(_buffer);

            if (write)
            {
                WriteOut(message);
            }
        }

        // We need to close the file and the stream
        public void close()
        {
            _streamWriter.Close();
            _streamWriter = null;
            _fs.Close();
        }

        /* -------------------------------------------------------
        * Write out function
        * Writes the string msg co the console without interupting 
        * the last line of the console (input line)
        * ------------------------------------------------------- */
        public void WriteOut(string msg)
        {
            // Acquire the current cursor position
            int inCol, inRow;
            inCol = Console.CursorLeft;
            inRow = Console.CursorTop;

            // Increase the console hight of one line
            outHeight++;
            Console.BufferHeight++;

            int outLines = getMsgRowCount(outCol, msg) + 1;     // How many lines will we write
            int outBottom = outRow + outLines;                  // Where will we end
            int scrollCount = outBottom - inRow + 1;            // How many lines do we have to move the last line
            // Move the last line (input) down to make room for the incoming message
            Console.MoveBufferArea(0, inRow, Console.BufferWidth, 1, 0, inRow + scrollCount);
            inRow += scrollCount;       // Update the current cursor position

            // Just to be sure, if the data won't fit into the console we add to the buffers
            if (outRow + outLines > outHeight)
            {
                outHeight += 100;
                Console.BufferHeight += 100;
            }
            Console.SetCursorPosition(outCol, outRow);      // Move the cursor to write the message
            Console.WriteLine(msg);         // Write the message
            outCol = Console.CursorLeft;    // Store that position for further use
            outRow = Console.CursorTop;     // Store that position for further use
            Console.SetCursorPosition(inCol, inRow);        // Move the cursor to the orginal position
        }

        /* -------------------------------------------------------
         * Get messages row count function
         * Determines how many rows of the console the string msg will occupie
         * ------------------------------------------------------- */
        public int getMsgRowCount(int startCol, string msg)
        {
            string[] lines = msg.Split('\n');
            int result = 0;
            foreach (string line in lines)
            {
                result += (startCol + line.Length) / Console.BufferWidth;
                startCol = 0;
            }
            return result + lines.Length - 2;
        }
    }
}
